import FYMessenger from '../Frame/FYMessenger';
import FYResMgr from '../Frame/Res/FYResMgr';
import FYBaseUI from '../Frame/UI/FYBaseUI';
import FYUIMgr from '../Frame/UI/FYUIMgr';
import { EnumUITween } from '../Frame/UI/UIEnum';
import SDKMgr from '../SDK/SDKMgr';

export default class UIPromotion extends FYBaseUI {
    /** 预制名 */
    public PrefabName: string = "P_UIPromotion";
    /** 显示动画 */
    public ShowTweenType: EnumUITween = EnumUITween.None;
    /** 隐藏动画 */
    public HideTweenType: EnumUITween = EnumUITween.None;

    /** 推广app列表 */
    private _btnAppArray: Array<cc.Node> = [];
    /** 按钮1 */
    private _btn1: cc.Node = null;
    /** 按钮2 */
    private _btn2: cc.Node = null;
    /** 按钮1文本 */
    private _labBtnName1: cc.Label = null;
    /** 按钮2文本 */
    private _labBtnName2: cc.Label = null;
    /** 按钮1点击回调 */
    private btn1Callback: Function = null;
    /** 按钮2点击回调 */
    private btn2Callback: Function = null;
    /** bannerID */
    private bannerId: string = "";

    //#region 生命周期


    //#endregion

    /**
     * 设置参数
     * @param param 
     * 参数1 按钮1的名字
     * 参数2 按钮1的点击回调
     * 参数3 按钮2的名字
     * 参数4 按钮2的点击回调
     * 参数5 推广app的图片列表["图片名"]
     * 参数6 推广app的appid列表["id"]
     * 参数7 bannerID
     */
    public async Init(param) {
        super.Init(param);
        this.initRoot();

        if (param) {
            if (param[0]) {
                // 按钮1的名字
                this._labBtnName1.string = param[0];
            }
            if (param[1]) {
                // 按钮1的点击事件
                this.btn1Callback = param[1];

            }
            if (param[2]) {
                // 按钮2的名字
                this._labBtnName2.string = param[2];
            }
            if (param[3]) {
                // 按钮2的点击事件
                this.btn2Callback = param[3];

            }
            if (param[4] && param[5]) {
                // 推广app的图片列表["图片名"]
                // 推广app的appid列表["id"]
                let a = [];
                let length = param[4].length;
                if (param[4].length > this._btnAppArray.length) {
                    length = this._btnAppArray.length;
                }
                for (let i = 0; i < this._btnAppArray.length; i++) {
                    this._btnAppArray[i].active = i < length;
                    if (i < length) {
                        this._btnAppArray[i].getComponent(cc.Sprite).spriteFrame = await FYResMgr.Instance.LoadRes(param[4][i], cc.SpriteFrame);
                        this._btnAppArray[i].on(cc.Node.EventType.TOUCH_END, () => { SDKMgr.Instance.navigateToMiniProgram(param[5][i]); }, this);
                    }
                }
            }
            if (param[6]) {
                SDKMgr.Instance.showBanner(param[6]);
            }
        }
    }

    protected addListener() {
        super.addListener();

        this._btn1.on(cc.Node.EventType.TOUCH_END, this.onClickBtn1, this);
        this._btn2.on(cc.Node.EventType.TOUCH_END, this.onClickBtn2, this);
    }

    protected removeListener() {
        super.removeListener();

        this._btn1.off(cc.Node.EventType.TOUCH_END, this.onClickBtn1, this);
        this._btn2.off(cc.Node.EventType.TOUCH_END, this.onClickBtn2, this);
    }

    private initRoot() {
        let appList = cc.find("Widget/AppList", this.node);
        appList.children.forEach(element => {
            if (element.name === "BtnApp") {
                this._btnAppArray.push(element);
            }
        });
        this._btn1 = cc.find("Widget/NeedJumpBtn/Btn1", this.node);
        this._btn2 = cc.find("Widget/NeedJumpBtn/Btn2", this.node);
        this._labBtnName1 = cc.find("LabBtn1", this._btn1).getComponent(cc.Label);
        this._labBtnName2 = cc.find("LabBtn2", this._btn2).getComponent(cc.Label);
    }

    //#region 回调函数

    private onClickBtn1() {
        if (this.btn1Callback) {
            this.btn1Callback();
        }
        FYUIMgr.Instance.CloseUI(UIPromotion);
    }

    private onClickBtn2() {
        if (this.btn2Callback) {
            this.btn2Callback();
        }
        FYUIMgr.Instance.CloseUI(UIPromotion);
    }

    //#endregion

}
